﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;

namespace MSBTC.ListWinForms
{
    public partial class ProjectListForm : Form
    {
        private string Datamanip;
        private int rowselect;
        MDIParent mdipar = new MDIParent();
        PMSContext dbcontext = new PMSContext();
        Customer dbcus = new Customer();
       
        public ProjectListForm()
        {
            InitializeComponent();
        }

        private void ProjectListForm_Load(object sender, EventArgs e)
        {
            RefreshDB();
        }
        #region functions
        private void RefreshDB()
        {
            //retrieve all data of projects in List
            var prj = (from c in dbcontext.Projects
                       select c).ToList();

            LVPrjListInfo.Items.Clear();
            if (prj.Count != 0)
            {
                foreach (var sqlPrj in prj)
                {
                    //add items in listview box
                    ListViewItem lvitems = LVPrjListInfo.Items.Add(sqlPrj.Projectid_PK.ToString());
                    lvitems.SubItems.Add(sqlPrj.Project_Name.ToString());
                    lvitems.SubItems.Add(sqlPrj.Description.ToString());
                    lvitems.SubItems.Add(sqlPrj.Location.ToString());
                    

                    var cus = dbcontext.Customers.Find(sqlPrj.Customerid_FK);
                    lvitems.SubItems.Add(cus.Customer_Name.ToString());

                   
                }
            }
            this.Refresh();
        }



        private void DisableFields()
        {
            //txtCname.Enabled = false;
            //txtAddr.Enabled = false;
            //txtCity.Enabled = false;
            //txtDesc.Enabled = false;
            //txtEadd.Enabled = false;
            //txtCnum.Enabled = false;

        }

        private void EnableFields()
        {
            //txtCname.Enabled = true;
            //txtAddr.Enabled = true;
            //txtCity.Enabled = true;
            //txtDesc.Enabled = true;
            //txtEadd.Enabled = true;
            //txtCnum.Enabled = true;

        }

        private void RefreshFields()
        {
            DisableFields();
            RefreshDB();

            //btnCNew.Enabled = true;
            //btnCDel.Enabled = false;
            //btnCEdit.Enabled = false;
            //btnClear.Enabled = true;
            //btnCSave.Enabled = false;
        }
        #endregion

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            var prj = (from p in dbcontext.Projects
                       where p.Project_Name.Contains(txtSearch.Text)
                       select p).ToList();

            LVPrjListInfo.Items.Clear();
            if (prj != null)
            {

                foreach (var sqlPrj in prj)
                {
                    ListViewItem lvitems = LVPrjListInfo.Items.Add(sqlPrj.Projectid_PK.ToString());
                    lvitems.SubItems.Add(sqlPrj.Project_Name.ToString());
                    lvitems.SubItems.Add(sqlPrj.Description.ToString());
                    lvitems.SubItems.Add(sqlPrj.Location.ToString());

                    var cus = dbcontext.Customers.Find(sqlPrj.Customerid_FK);
                    lvitems.SubItems.Add(cus.Customer_Name.ToString());


                }
                if (txtSearch.Text == "")
                {
                    RefreshDB();
                }
            }
        }

      

    }
}
